package com.ymatou.api.marketingseller.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.api.marketingseller.service.GetPromotionByIdCall;
import com.ymatou.api.marketingseller.service.GetPromotionProductByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.database.model.YmtSellerPromotion;
import com.ymttest.database.sqlwapper.YmtSellerPromotionIMapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;


/**
 * 
 * @author chencan
 *
 */
public class Ts_GetPromotionProductById {
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据促销活动Id获取指定商品Id");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetPromotionProductByIdCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_001() {
		Logger.start(true, "根据促销活动Id获取指定商品Id");
		try {
			
			List<YmtSellerPromotion> sellerPromotionIDlist = new YmtSellerPromotionIMapper().selectPromotionState(3383);
			YmtSellerPromotion promotionstate =sellerPromotionIDlist.get(0);
			int sellerId = promotionstate.getSellerId();
			int promotionId = promotionstate.getPromotionId();
			
			// 请求参数
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("SellerId",sellerId);
			map.put("PromotionId",promotionId);
			JSONObject updateMap = new GetPromotionProductByIdCall().callAndGetReturnData(map);
			
			JSONArray productIds = updateMap.getJSONObject("Data").getJSONArray("ProductIds");
			int code =updateMap.getInt("Code");//断言code是否等于200
			
			//断言
			Logger.verifyEquals(200, code, "响应Code是否200");
			Logger.verifyNotNull(productIds, "返回促销数据不为空");
			
		} catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_002() {
		Logger.start(true, "根据促销活动Id获取指定商品Id-无指定商品");
		try {
			
			int sellerId = 20354944;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			
			// 创建活动
			Map<String,Object> updateMap = new HashMap<String,Object>();
			updateMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			updateMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(updateMap);
			
			// 获取新促销活动信息
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			JSONObject jsonObject = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
			
			// 获取返回报文
			JSONArray promotionInfo = jsonObject.getJSONObject("Data").getJSONArray("ProductIds");
			int code =jsonObject.getInt("Code");
			int size = promotionInfo.length();      
			
			// 断言
			Logger.verifyEquals(200, code, "响应Code是否200");
			Logger.verifyEquals(0, size, "无指定商品，结果为空");
			
		} catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_003() {
		Logger.start(true, "根据促销活动Id不存在");
		try {
			
			// 请求参数
			int sellerId = 20354944;
			int promotionId=00000;
			JSONObject jsonObject = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
			
			// 获取返回报文
			JSONArray promotionInfo = jsonObject.getJSONObject("Data").getJSONArray("ProductIds");
			int code =jsonObject.getInt("Code");
			int size = promotionInfo.length();      
			
			// 断言
			Logger.verifyEquals(200, code, "响应Code是否200");
			Logger.verifyEquals(0, size, "活动不存在，结果为空");
			
		} catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_004() {
		Logger.start(true, "根据促销活动与买手不匹配");
		try {
			
			// 请求参数
			int sellerId = 20354944;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			
			// 创建活动
			Map<String,Object> updateMap = new HashMap<String,Object>();
			updateMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			updateMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(updateMap);
			
			// 获取新促销活动信息
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			JSONObject jsonObject = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", 3333,"promotionId", promotionId));
			
			// 获取返回报文
			JSONArray promotionInfo = jsonObject.getJSONObject("Data").getJSONArray("ProductIds");
			int code =jsonObject.getInt("Code");
			int size = promotionInfo.length();      
			
			// 断言
			Logger.verifyEquals(200, code, "响应Code是否200");
			Logger.verifyEquals(0, size, "活动与买手不匹配，结果为空");
			
		} catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
	}
}
